<?php
class ControllerAccountRegister extends Controller {
	private $error = array();

	public function index() {

		$this->load->model('account/customer');

	    if (!$this->actionCheck(false,true)) {
            return;
	    }

		if ($this->validate()) {
			$this->model_account_customer->addCustomer($this->request->post);

			unset($this->session->data['phone_valide_code']);

			// Clear any previous login attempts for unregistered accounts.
			$this->model_account_customer->deleteLoginAttempts($this->request->post['phone']);

			$this->customer->login($this->request->post['phone'], $this->request->post['password']);

			unset($this->session->data['guest']);


			// Add to activity log
			$this->load->model('account/activity');

			$activity_data = array(
				'customer_id' => $this->customer->getId(),
				'name'        => $this->request->post['firstname']
			);

			$customer_info = $this->customer->getCustomerInfo();


			$this->model_account_activity->addActivity('register', $activity_data);

			if(isset($this->session->data['wx_unionid']))
			{
			    $this->load->model('account/social');
			    $this->model_account_social->bind(array('uid'=>$this->session->data['wx_unionid'],'type'=>1));
			}

			$this->response->setDataResult(1,"",$customer_info);
			return;
		}


		if (count($this->error)>0) {
			$error_warning = array_shift($this->error);
		} else {
			$error_warning = '';
		}
		$this->response->setDataResult(0,$error_warning,null);
		return;
	}

	public function phoneExist()
	{
	    $this->load->model('account/customer');

	    if (!$this->actionCheck(false,true)) {
	        return;
	    }

	    if ($this->phoneValidate()) {
	        $this->response->setDataResult(1,"",null);
	        return;
	    }

	    if (count($this->error)>0) {
	        $error_warning = array_shift($this->error);
	    } else {
	        $error_warning = '';
	    }
	    $this->response->setDataResult(0,$error_warning,null);
	    return;
	}

	public function validate() {
	    if(isset($this->request->post['code']))
	    {
	        if( !preg_match('/^\d{4}$/i', $this->request->post['code']))
	        {
	            $this->error['code'] = '请输入4位数字短信验证码！';
	        }
	        else if(!isset($this->session->data['phone_valide_code']))
	        {
	            $this->error['code'] = '无效的短信验证码！';
	        }
	        else if(!strpos($this->session->data['phone_valide_code'],$this->request->post['code']))
	        {
	            $this->error['code'] = '短信验证码错误！';
	        }
	    }


	    if (!preg_match('/^[1][358]\d{9}$/', $this->request->post['phone'])) {
	        $this->error['phone'] = '手机号不合法！';
	    }
	    else
	    {
	        $this->request->post['email'] = $this->request->post['phone'];
	        $this->request->post['telephone'] = $this->request->post['phone'];
	    }

		if ((utf8_strlen(trim($this->request->post['nickname'])) < 1) || (utf8_strlen(trim($this->request->post['nickname'])) > 32)) {
			$this->error['nickname'] = '请输入昵称！';
		}
		else
		{
		    $this->request->post['firstname'] = $this->request->post['nickname'];
		}

		if ((utf8_strlen($this->request->post['password']) < 6) || (utf8_strlen($this->request->post['password']) > 20)) {
		    $this->error['password'] = '请输入6-20位密码！';
		}

		if(isset($this->request->post['is_cooker']))
		{
		    if($this->config->get('commonconfig_common_cookerregisterreview')==1)
		    {
		        $this->request->post['is_cooker'] = 2;
		    }
		    else
		    {
		        $this->request->post['is_cooker'] = 1;
		    }

		    if ((utf8_strlen(trim($this->request->post['name'])) < 1) || (utf8_strlen(trim($this->request->post['name'])) > 32)) {
		        $this->error['name'] = '请输入厨师姓名！';
		    }
		    else
		   {
		       $this->request->post['lastname'] = $this->request->post['name'];
		    }

		    if ((utf8_strlen(trim($this->request->post['description'])) < 1) || (utf8_strlen(trim($this->request->post['description'])) > 32)) {
		        $this->error['description'] = '请输入自我介绍！';
		    }

		    if ((utf8_strlen(trim($this->request->post['province_id'])) < 1)) {
		        $this->error['province_id'] = '请选择城市（省）！';
		    }
		    else
		    {
		        $this->request->post['country'] = $this->request->post['province_id'];
		    }

		    if ((utf8_strlen(trim($this->request->post['city_id'])) < 1)) {
		        $this->error['city_id'] = '请选择城市（市）！';
		    }
		    else
		    {
		        $this->request->post['zone'] = $this->request->post['city_id'];
		    }

		    if ((utf8_strlen(trim($this->request->post['years'])) < 1)) {
		        $this->error['years'] = '请选择做饭经验！';
		    }
		}
		else
		{
		    $this->request->post['is_cooker'] = 0;
		    $this->request->post['lastname'] = '';
		    $this->request->post['description'] = '';
		    $this->request->post['country'] = 0;
		    $this->request->post['zone'] = 0;
		    $this->request->post['years'] = 0;
		}

		if(!$this->error)
		{
		    if ($this->model_account_customer->getTotalCustomersByPhone($this->request->post['phone'])) {
		        $this->error['warning'] = '手机号已注册！';
		    }
		}

		return !$this->error;
	}

	private function phoneValidate()
	{
	    if (!preg_match('/^\d{11,16}$/i', $this->request->post['phone'])) {
	        $this->error['phone'] = '手机号不合法！';
	    }

	    if(!$this->error)
	    {
	        if ($this->model_account_customer->getTotalCustomersByPhone($this->request->post['phone'])) {
	            $this->error['warning'] = '手机号已注册！';
	        }
	    }

	    return !$this->error;
	}

}